//
//  DMAddAddressVC.m
//  DistributionMall
//
//  Created by JHnoplan on 2018/10/16.
//  Copyright © 2018年 JHnoplan. All rights reserved.
//

#import "DMAddAddressVC.h"
#import "AddressProvinceCityController.h"

#import "KMineRequest.h"
@interface DMAddAddressVC ()<UITextViewDelegate>
{
    BOOL isDefault;//是否设为默认
}
@property (weak, nonatomic) IBOutlet UITextField *nameTF;
@property (weak, nonatomic) IBOutlet UITextField *phoneTF;
@property (weak, nonatomic) IBOutlet UITextField *addressTF;
@property (weak, nonatomic) IBOutlet UITextField *codeTF;
@property (weak, nonatomic) IBOutlet UITextView *detailTV;
@property (weak, nonatomic) IBOutlet UILabel *placeholderLb;
@property (weak, nonatomic) IBOutlet UIImageView *defaultImage;

@property (nonatomic,strong) AddressProvinceCityResultModel * addressModel;

@end

@implementation DMAddAddressVC

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    [self startInit];
    [self setLastData];

}

-(void)startInit {
    self.navigationItem.title = @"添加收货地址";
    self.view.backgroundColor = DMDefaultColor;
    isDefault = NO;
    _detailTV.delegate = self;
}

-(void)textViewDidChange:(UITextView *)textView {
    if (textView.text.length == 0) {
        _placeholderLb.hidden = NO;
    }else {
        _placeholderLb.hidden = YES;
    }
}

//设为默认
- (IBAction)defaultClick:(UIButton *)sender {
    if (isDefault) {
        isDefault = NO;
        _defaultImage.image = [UIImage imageNamed:@"noselet"];
    } else {
        isDefault = YES;
        _defaultImage.image = [UIImage imageNamed:@"Select"];
    }
}
//保存地址
- (IBAction)saveClick:(UIButton *)sender {
 
    if (self.nameTF.text.length == 0) {
        [JLProgressHUDManager showErrorHud:@"请填写收货人名字"];
        return;
    }else if (self.phoneTF.text.length == 0) {
        [JLProgressHUDManager showErrorHud:@"请填写收货人电话号码"];
        return;
    }else if (self.addressModel == nil) {
        [JLProgressHUDManager showErrorHud:@"请选择所在地区"];
        return;
    }
//    else if (self.codeTF.text.length == 0) {
//        [JLProgressHUDManager showErrorHud:@"请填写邮政编码"];
//        return;
//    }
    else if (self.detailTV.text.length == 0) {
        [JLProgressHUDManager showErrorHud:@"请填写详细地址"];
        return;
    }
    
    NSString * isDefaultStr = isDefault?@"Y":@"N";

    MJWeakSelf
    [JLProgressHUDManager showLoadingHud];
    if (self.model) {
        
        KMineRequest * request = [[KMineRequest alloc]initChangeAddressWithAdressResultModel:self.addressModel detail:self.detailTV.text isDefault:isDefaultStr name:self.nameTF.text phone:self.phoneTF.text id:self.model.id];
        
        
        [request newStartRequestWithCompletion:^(KBaseModel *baseModel) {
            
            [JLProgressHUDManager showSuccessHud:@"修改地址成功"];
            [weakSelf.navigationController popViewControllerAnimated:YES];
            
        } failure:^(KBaseModel *errorModel) {
            [JLProgressHUDManager showErrorHud:errorModel.msg];
        }];
        
    }else{
        KMineRequest * request = [[KMineRequest alloc]initAddAddressWithAdressResultModel:self.addressModel detail:self.detailTV.text isDefault:isDefaultStr name:self.nameTF.text phone:self.phoneTF.text];
        
        [request newStartRequestWithCompletion:^(KBaseModel *baseModel) {
            [JLProgressHUDManager showSuccessHud:@"添加地址成功"];
            [weakSelf.navigationController popViewControllerAnimated:YES];
            
        } failure:^(KBaseModel *errorModel) {
            [JLProgressHUDManager showErrorHud:errorModel.msg];
        }];
    }
}
//选择地址
- (IBAction)selectAreaClick:(UIButton *)sender {
    
    AddressProvinceCityController * vc = [[AddressProvinceCityController alloc]initWithResultModel:nil AddressVCType:AddressVCTypeProvince parentId:nil titleName:nil lastVCType:AddressVCTypeArea];
    
    MJWeakSelf
    [vc setAddressCompleted:^(AddressProvinceCityResultModel *resultModel) {
        weakSelf.addressModel = resultModel;
        weakSelf.addressTF.text = [NSString stringWithFormat:@"%@%@%@",resultModel.province,resultModel.city,resultModel.area];
    }];
    [self.navigationController pushViewController:vc animated:YES];
}


//编辑时设置默认数据
-(void)setLastData{
    if (self.model) {
        self.addressModel = [[AddressProvinceCityResultModel alloc]init];
        
        self.addressModel.provinceId = self.model.prRid;
        self.addressModel.province = self.model.prName;
        self.addressModel.cityId = self.model.ciRid;
        self.addressModel.city = self.model.ciName;
        self.addressModel.areaId = self.model.arRid;
        self.addressModel.area = self.model.arName;
        
        self.nameTF.text = self.model.person;
        self.phoneTF.text = self.model.mobile;
        self.addressTF.text = [NSString stringWithFormat:@"%@%@%@",self.model.prName,self.model.ciName,self.model.arName];
        self.detailTV.text = self.model.addr;
        self.placeholderLb.hidden = self.detailTV.text.length != 0;

        isDefault = ![self.model.isDefault isEqualToString:@"Y"];
        [self defaultClick:nil];
    }
}

@end
